package org.FaPe3D.core.languages
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.Dictionary;
	
	import org.FaPe3D.*;
	import org.FaPe3D.core.parsers.Language;
	import org.FaPe3D.core.settings.*;
	
	public class Languages extends MovieClip
	{
		private var _id:String;
		
		private var _bulkLoader:BulkLoader;
		private var _LOADER_KEY = 'language_';
		private var _languages:Dictionary;
		private var _defaultLanguage:Dictionary;
		
		public function Languages(id:String)
		{
			_id = id;
			_languages = new Dictionary(true);
			_defaultLanguage = new Dictionary(true);
			_LOADER_KEY = _LOADER_KEY + _id;
			
			// cretae bulkLoader instance
			_bulkLoader = new BulkLoader('FAPE3D_' + 'languages' + _id);
			// add file to bulk loader
			
			_bulkLoader.add( Adapter.getAdapter(_id).settings.setting['path.languages'] , {type:"xml", id: _LOADER_KEY});
			
			// start loading
			_bulkLoader.start();
			// setup events
			_bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, onCompelete);
			_bulkLoader.addEventListener(BulkLoader.PROGRESS, loadingProgress);
			_bulkLoader.addEventListener(BulkLoader.ERROR, loadingError)
		}
		
		private function onCompelete(e:*):void
		{
			// loading compeleted.
			
			// get content and clear memory
			var xml:XML = _bulkLoader.getXML( _LOADER_KEY, true );
			languages = Language.parse( xml );
			// remove bulkloader instance
			_bulkLoader.removeAll();
			// build defaultLanguage Dictionary 
			changeDefaultLanguage = defaultLanguageName;
			//	
			this.dispatchEvent(new Event(Event.COMPLETE));		
		}
		
		public function get defaultLanguageName():String
		{
			var name:String = '';
			
			for(var i:uint = 0; i< Adapter.getAdapter(_id).settings.setting['languages'].length; i++)
			{
				if(Adapter.getAdapter(_id).settings.setting['languages'][i].default)
				{
					name = Adapter.getAdapter(_id).settings.setting['languages'][i].name;
					break;
				}
			}
			
			return name;
		}
		
		public function set changeDefaultLanguage(languageName:String):void
		{
			// change default language
			for(var i:uint = 0; i< Adapter.getAdapter(_id).settings.setting['languages'].length; i++)
			{
				Adapter.getAdapter(_id).settings.setting['languages'][i].default = (Adapter.getAdapter(_id).settings.setting['languages'].name == languageName) ? true : false;
			}
			//
			Adapter.getAdapter(_id).settings.setting['languages.' + languageName].default = true;
			
			// uopdate _defaultLanguage dictionary
			var dic_default:Dictionary = new Dictionary(true);
			var k:String;
			for(var j:String in _languages)
			{
				k = j.toString();
				
				if(!(k.indexOf( languageName + '.' )))
				{
					dic_default[k.replace(languageName + '.', '')] = _languages[j];
				} 
			}
			// set default language dictionary
			_defaultLanguage = dic_default;
		}
		
		public function get languages():Dictionary
		{
			return _languages;
		}
		
		public function set languages(dic:Dictionary):void
		{
			_languages = dic;
		}
		
		public function get defaultLanguage():Dictionary
		{
			return _defaultLanguage;
		}
		
		private function loadingProgress(e:*):void
		{
//			trace('loading progress', e.bytesLoaded , '/', e.bytesTotal);
		}
		
		private function loadingError(e:*):void
		{
			trace('error in loading xml file. (language)' , e);
		}

	}
}